#!/bin/bash

TARGET_PRODUCT=imx51_f101

TARGET_BUILD_VARIANT=user
TARGET_BUILD_VARIANT_ENG=eng


PWD_DIR=$(pwd)
ANDROID_DIR=$PWD_DIR
BUILD_FLAGS=
BUILD_FLAGS_ALL="-a"
OPTION=
BUILD_FLAGS_ENG="-eng"
BUILD_FLAGS_SDK="-sdk"


function Usage()
{
  echo "buildit - building script android platform"
  echo "Usage: ./buildit [-option]"
  echo "  -a or no option: build whole images (android)"
  echo "  -eng : build android system eng version for root permission"
  echo "  -sdk : build android sdk"
#  echo "  -k : build kernel"
#  echo "  -u : build uboot"
#  echo "  -t : build selftest"
  echo "  -h : show this message"
#  echo "  -j : build support multiprocesor"	
  echo

  exit 1
}


function set_env
{
  export CROSS_COMPILE=$(pwd)/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-
}

#report error
function report_error()
{

	echo
        echo "*****************************************************"
        echo "*       Build Error!!!			          *"
        echo "*****************************************************"
        echo

	
	END_TIME=$(date +%s)
        let ELAPSED_TIME=$END_TIME-$START_TIME
        echo 
        echo Fail to build android.
        echo Total compile time is $ELAPSED_TIME seconds.
        echo
}


#build android eng version for root permission
function build_android_eng()
{
	START_TIME=$(date +%s)

	echo
        echo "*****************************************************"
        echo " Build android system eng version for root permission"
        echo "*****************************************************"
        echo

	cd $ANDROID_DIR

	set_env
	
	echo make $BUILD_FLAGS $TARGET_PRODUCT-$TARGET_BUILD_VARIANT_ENG
        source build/envsetup.sh
        lunch $TARGET_PRODUCT-$TARGET_BUILD_VARIANT_ENG
        make $BUILD_FLAGS
	if [ $? != 0 ]; then
		report_error
		exit $?
	fi

	END_TIME=$(date +%s)
        let ELAPSED_TIME=$END_TIME-$START_TIME
        echo 
        echo Success to build android.
        echo Total compile time is $ELAPSED_TIME seconds.
        echo
}

#build android sdk
function build_android_sdk()
{
	START_TIME=$(date +%s)

	echo
        echo "*****************************************************"
        echo " 			Build android sdk	 	  *"
        echo "*****************************************************"
        echo

	cd $ANDROID_DIR

	set_env
	
	echo "make sdk"
        source build/envsetup.sh
        lunch $TARGET_PRODUCT-$TARGET_BUILD_VARIANT
        make sdk
	if [ $? != 0 ]; then
		report_error
		exit $?
	fi

	END_TIME=$(date +%s)
        let ELAPSED_TIME=$END_TIME-$START_TIME
        echo 
        echo Success to build android.
        echo Total compile time is $ELAPSED_TIME seconds.
        echo
}

function build_android()
{
	START_TIME=$(date +%s)

	echo
        echo "*****************************************************"
        echo "*       Build android system		          *"
        echo "*****************************************************"
        echo

	cd $ANDROID_DIR

	set_env
	
	echo make $BUILD_FLAGS $TARGET_PRODUCT-$TARGET_BUILD_VARIANT
        source build/envsetup.sh
        lunch $TARGET_PRODUCT-$TARGET_BUILD_VARIANT
        make $BUILD_FLAGS
	if [ $? != 0 ]; then
		report_error
		exit $?
	fi

	END_TIME=$(date +%s)
        let ELAPSED_TIME=$END_TIME-$START_TIME
        echo 
        echo Success to build android.
        echo Total compile time is $ELAPSED_TIME seconds.
        echo
}

# start run here
if [ $1 ] ; then
	if [ $1 = $BUILD_FLAGS_ALL -o $1 = $BUILD_FLAGS_ENG -o $1 = $BUILD_FLAGS_SDK ] ; then
	OPTION=$1
	BUILD_FLAGS+="$2 $3 $4 $5 $6"
	
		if [ $2 ] ; then
			if [ ${2:0:2} = "-j" ] ; then
				if [ ${2:2:3} -gt 16 ] ; then
					BUILD_FLAGS="-j16 $3 $4 $5 $6"
				fi
			fi
		fi
	
	else if [ ${1:0:2} = "-j" ] ; then
		BUILD_FLAGS+="$1 $2 $3 $4 $5 $6"
		OPTION=-a

		if [ ${1:2:3} -gt 16 ] ; then
			BUILD_FLAGS="-j16 $2 $3 $4 $5 $6"
		fi
	else
		BUILD_FLAGS="$1 $2 $3 $4 $5 $6"
		Usage
	fi
	fi
else
	OPTION=-a
        BUILD_FLAGS+="$1 $2 $3 $4 $5 $6"
fi


case "$OPTION" in
	-a)
		build_android
		;;
	$BUILD_FLAGS_ENG)
		build_android_eng
		;;
	$BUILD_FLAGS_SDK)
	build_android_sdk
		;;
	*)
		Usage
		;;
esac

exit 0
